home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / prim / minibuf.el.z / minibuf.el
Encoding:
Text File  |  1998-05-21  |  83.2 KB  |  2,134 lines

  1. ;;; minibuf.el --- Minibuffer functions for XEmacs
  2.  
  3. ;; Copyright (C) 1992, 1993, 1994, 1997 Free Software Foundation, Inc.
  4. ;; Copyright (C) 1995 Tinker Systems
  5. ;; Copyright (C) 1995, 1996 Ben Wing
  6.  
  7. ;; Author: Richard Mlynarik
  8. ;; Created: 2-Oct-92
  9. ;; Maintainer: XEmacs Development Team
  10. ;; Keywords: internal
  11.  
  12. ;; This file is part of XEmacs.
  13.  
  14. ;; XEmacs is free software; you can redistribute it and/or modify it
  15. ;; under the terms of the GNU General Public License as published by
  16. ;; the Free Software Foundation; either version 2, or (at your option)
  17. ;; any later version.
  18.  
  19. ;; XEmacs is distributed in the hope that it will be useful, but
  20. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  21. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  22. ;; General Public License for more details.
  23.  
  24. ;; You should have received a copy of the GNU General Public License
  25. ;; along with XEmacs; see the file COPYING.  If not, write to the 
  26. ;; Free Software Foundation, 59 Temple Place - Suite 330,
  27. ;; Boston, MA 02111-1307, USA.
  28.  
  29. ;;; Synched up with: all the minibuffer history stuff is synched with
  30. ;;; 19.30.  Not sure about the rest.
  31.  
  32. ;;; Commentary:
  33.  
  34. ;; Written by Richard Mlynarik 2-Oct-92
  35.  
  36. ;; 06/11/1997 -  Use char-(after|before) instead of
  37. ;;  (following|preceding)-char. -slb
  38.  
  39. ;;; Code:
  40.  
  41. (defgroup minibuffer nil
  42.   "Minibuffer customizations"
  43.   :group 'environment)
  44.  
  45.  
  46. (defcustom insert-default-directory t
  47.  "*Non-nil means when reading a filename start with default dir in minibuffer."
  48.  :type 'boolean
  49.  :group 'minibuffer)
  50.  
  51. (defcustom minibuffer-history-uniquify t
  52.   "*Non-nil means when adding an item to a minibuffer history, remove
  53. previous occurances of the same item from the history list first,
  54. rather than just consing the new element onto the front of the list."
  55.   :type 'boolean
  56.   :group 'minibuffer)
  57.  
  58. (defvar minibuffer-completion-table nil
  59.   "Alist or obarray used for completion in the minibuffer.
  60. This becomes the ALIST argument to `try-completion' and `all-completions'.
  61.  
  62. The value may alternatively be a function, which is given three arguments:
  63.   STRING, the current buffer contents;
  64.   PREDICATE, the predicate for filtering possible matches;
  65.   CODE, which says what kind of things to do.
  66. CODE can be nil, t or `lambda'.
  67. nil means to return the best completion of STRING, nil if there is none,
  68.   or t if it is was already a unique completion.
  69. t means to return a list of all possible completions of STRING.
  70. `lambda' means to return t if STRING is a valid completion as it stands.")
  71.  
  72. (defvar minibuffer-completion-predicate nil
  73.   "Within call to `completing-read', this holds the PREDICATE argument.")
  74.  
  75. (defvar minibuffer-completion-confirm nil
  76.   "Non-nil => demand confirmation of completion before exiting minibuffer.")
  77.  
  78. (defvar minibuffer-confirm-incomplete nil
  79.   "If true, then in contexts where completing-read allows answers which
  80. are not valid completions, an extra RET must be typed to confirm the
  81. response.  This is helpful for catching typos, etc.")
  82.  
  83. (defcustom completion-auto-help t
  84.   "*Non-nil means automatically provide help for invalid completion input."
  85.   :type 'boolean
  86.   :group 'minibuffer)
  87.  
  88. (defcustom enable-recursive-minibuffers nil
  89.   "*Non-nil means to allow minibuffer commands while in the minibuffer.
  90. More precisely, this variable makes a difference when the minibuffer window
  91. is the selected window.  If you are in some other window, minibuffer commands
  92. are allowed even if a minibuffer is active."
  93.   :type 'boolean
  94.   :group 'minibuffer)
  95.  
  96. (defcustom minibuffer-max-depth 1
  97.   ;; See comment in #'minibuffer-max-depth-exceeded
  98.   "*Global maximum number of minibuffers allowed;
  99. compare to enable-recursive-minibuffers, which is only consulted when the
  100. minibuffer is reinvoked while it is the selected window."
  101.   :type '(choice integer
  102.          (const :tag "Indefinite" nil))
  103.   :group 'minibuffer)
  104.  
  105. ;; Moved to C.  The minibuffer prompt must be setup before this is run
  106. ;; and that can only be done from the C side.
  107. ;(defvar minibuffer-setup-hook nil
  108. ;  "Normal hook run just after entry to minibuffer.")
  109.  
  110. (defvar minibuffer-exit-hook nil
  111.   "Normal hook run just after exit from minibuffer.")
  112.  
  113. (defvar minibuffer-help-form nil
  114.   "Value that `help-form' takes on inside the minibuffer.")
  115.  
  116. (defvar minibuffer-local-map
  117.   (let ((map (make-sparse-keymap 'minibuffer-local-map)))
  118.     map)
  119.   "Default keymap to use when reading from the minibuffer.")
  120.  
  121. (defvar minibuffer-local-completion-map
  122.   (let ((map (make-sparse-keymap 'minibuffer-local-completion-map)))
  123.     (set-keymap-parents map (list minibuffer-local-map))
  124.     map)
  125.   "Local keymap for minibuffer input with completion.")
  126.  
  127. (defvar minibuffer-local-must-match-map
  128.   (let ((map (make-sparse-keymap 'minibuffer-must-match-map)))
  129.     (set-keymap-parents map (list minibuffer-local-completion-map))
  130.     map)
  131.   "Local keymap for minibuffer input with completion, for exact match.")
  132.  
  133. ;; (define-key minibuffer-local-map "\C-g" 'abort-recursive-edit)
  134. (define-key minibuffer-local-map "\C-g" 'minibuffer-keyboard-quit) ;; moved here from pending-del.el
  135. (define-key minibuffer-local-map "\r" 'exit-minibuffer)
  136. (define-key minibuffer-local-map "\n" 'exit-minibuffer)
  137.  
  138. ;; Historical crock.  Unused by anything but user code, if even that
  139. ;(defvar minibuffer-local-ns-map
  140. ;  (let ((map (make-sparse-keymap 'minibuffer-local-ns-map)))
  141. ;    (set-keymap-parents map (list minibuffer-local-map))
  142. ;    map)
  143. ;  "Local keymap for the minibuffer when spaces are not allowed.")
  144. ;(define-key minibuffer-local-ns-map [space] 'exit-minibuffer)
  145. ;(define-key minibuffer-local-ns-map [tab] 'exit-minibuffer)
  146. ;(define-key minibuffer-local-ns-map [?\?] 'self-insert-and-exit)
  147.  
  148. (define-key minibuffer-local-completion-map "\t" 'minibuffer-complete)
  149. (define-key minibuffer-local-completion-map " " 'minibuffer-complete-word)
  150. (define-key minibuffer-local-completion-map "?" 'minibuffer-completion-help)
  151. (define-key minibuffer-local-must-match-map "\r" 'minibuffer-complete-and-exit)
  152. (define-key minibuffer-local-must-match-map "\n" 'minibuffer-complete-and-exit)
  153.  
  154. (define-key minibuffer-local-map "\M-n" 'next-history-element)
  155. (define-key minibuffer-local-map "\M-p" 'previous-history-element)
  156. (define-key minibuffer-local-map '[next]  "\M-n")
  157. (define-key minibuffer-local-map '[prior] "\M-p")
  158. (define-key minibuffer-local-map "\M-r" 'previous-matching-history-element)
  159. (define-key minibuffer-local-map "\M-s" 'next-matching-history-element)
  160. (define-key minibuffer-local-must-match-map [next] 
  161.   'next-complete-history-element)
  162. (define-key minibuffer-local-must-match-map [prior]
  163.   'previous-complete-history-element)
  164.  
  165. ;; This is an experiment--make up and down arrows do history.
  166. (define-key minibuffer-local-map [up] 'previous-history-element)
  167. (define-key minibuffer-local-map [down] 'next-history-element)
  168. (define-key minibuffer-local-completion-map [up] 'previous-history-element)
  169. (define-key minibuffer-local-completion-map [down] 'next-history-element)
  170. (define-key minibuffer-local-must-match-map [up] 'previous-history-element)
  171. (define-key minibuffer-local-must-match-map [down] 'next-history-element)
  172.  
  173. (defvar read-expression-map (let ((map (make-sparse-keymap
  174.                     'read-expression-map)))
  175.                               (set-keymap-parents map
  176.                           (list minibuffer-local-map))
  177.                               (define-key map "\M-\t" 'lisp-complete-symbol)
  178.                               map)
  179.   "Minibuffer keymap used for reading Lisp expressions.")
  180.  
  181. (defvar read-shell-command-map
  182.   (let ((map (make-sparse-keymap 'read-shell-command-map)))
  183.     (set-keymap-parents map (list minibuffer-local-map))
  184.     (define-key map "\t" 'comint-dynamic-complete)
  185.     (define-key map "\M-\t" 'comint-dynamic-complete)
  186.     (define-key map "\M-?" 'comint-dynamic-list-completions)
  187.     map)
  188.   "Minibuffer keymap used by shell-command and related commands.")
  189.  
  190. (defcustom use-dialog-box t
  191.   "*Variable controlling usage of the dialog box.
  192. If nil, the dialog box will never be used, even in response to mouse events."
  193.   :type 'boolean
  194.   :group 'minibuffer)
  195.  
  196. (defcustom minibuffer-electric-file-name-behavior t
  197.   "*If non-nil, slash and tilde in certain places cause immediate deletion.
  198. These are the same places where this behavior would occur later on anyway,
  199. in `substitute-in-file-name'."
  200.   :type 'boolean
  201.   :group 'minibuffer)
  202.  
  203. (defun minibuffer-electric-slash ()
  204.   ;; by Stig@hackvan.com
  205.   ;; modified heavily by skip@calendar.com
  206.   (interactive)
  207.   (and minibuffer-electric-file-name-behavior
  208.      (eq ?/ (char-before (point)))
  209.      (not (save-excursion
  210.           (goto-char (point-min))
  211.       (and (or
  212.         ;; just an efs host frag & root dir?
  213.         (and (looking-at "^/.+:/$")
  214.          (progn
  215.            (delete-region (point) (point-max))
  216.            t))
  217.         ;; an efs host frag and something else?
  218.         (and (looking-at "^/.+:")
  219.              (re-search-forward "^/.+:" nil t)
  220.              (progn
  221.                (delete-region (point) (point-max))
  222.                t))))))
  223.      (not (eq (point) (1+ (point-min)))) ; permit `//hostname/path/to/file'
  224.      (not (eq ?: (char-after (- (point) 2)))) ; permit `http://url/goes/here'
  225.      (delete-region (point-min) (point)))
  226.   (insert ?/))
  227.  
  228. (defun minibuffer-electric-tilde ()
  229.   ;; by Stig@hackvan.com
  230.   ;; modified heavily by skip@calendar.com
  231.   (interactive)
  232.   (and minibuffer-electric-file-name-behavior
  233.      (eq ?/ (char-before (point)))
  234.      (not (save-excursion
  235.           (goto-char (point-min))
  236.       (and (or
  237.         ;; just an efs host frag & root or user dir?
  238.         (and (looking-at "^/.+:\\(~[^/]*\\)?/$")
  239.          (progn
  240.            (delete-region (point) (point-max))
  241.            t))
  242.         ;; an efs host frag and something else?
  243.         (and (looking-at "^/.+:")
  244.              (re-search-forward "^/.+:" nil t)
  245.              (progn
  246.                (delete-region (point) (point-max))
  247.                t))))))
  248.      (not (save-excursion (search-backward "//" nil t)))
  249.      (delete-region (point-min) (point)))
  250.   (insert ?~))
  251.  
  252. (defvar read-file-name-map
  253.   (let ((map (make-sparse-keymap 'read-file-name-map)))
  254.     (set-keymap-parents map (list minibuffer-local-completion-map))
  255.     (define-key map "/" 'minibuffer-electric-slash)
  256.     (define-key map "~" 'minibuffer-electric-tilde)
  257.     map
  258.     ))
  259.  
  260. (defvar read-file-name-must-match-map
  261.   (let ((map (make-sparse-keymap 'read-file-name-map)))
  262.     (set-keymap-parents map (list minibuffer-local-must-match-map))
  263.     (define-key map "/" 'minibuffer-electric-slash)
  264.     (define-key map "~" 'minibuffer-electric-tilde)
  265.     map
  266.     ))
  267.  
  268. (defun minibuffer-keyboard-quit ()
  269.   "Abort recursive edit.
  270. If `zmacs-regions' is true, and the zmacs region is active in this buffer,
  271. then this key deactivates the region without beeping."
  272.   (interactive)
  273.   (if (and (region-active-p)
  274.        (eq (current-buffer) (zmacs-region-buffer)))
  275.       ;; pseudo-zmacs compatibility: don't beep if this ^G is simply
  276.       ;; deactivating the region.  If it is inactive, beep.
  277.       nil
  278.     (abort-recursive-edit)))
  279.  
  280. ;;;; Guts of minibuffer invocation
  281.  
  282. ;;#### The only things remaining in C are
  283. ;; "Vminibuf_prompt" and the display junk
  284. ;;  "minibuf_prompt_width" and "minibuf_prompt_pix_width"
  285. ;; Also "active_frame", though I suspect I could already
  286. ;;   hack that in Lisp if I could make any sense of the
  287. ;;   complete mess of frame/frame code in XEmacs.
  288. ;; Vminibuf_prompt could easily be made Lisp-bindable.
  289. ;;  I suspect that minibuf_prompt*_width are actually recomputed
  290. ;;  by redisplay as needed -- or could be arranged to be so --
  291. ;;  and that there could be need for read-minibuffer-internal to
  292. ;;  save and restore them.
  293. ;;#### The only other thing which read-from-minibuffer-internal does
  294. ;;  which we can't presently do in Lisp is move the frame cursor
  295. ;;  to the start of the minibuffer line as it returns.  This is
  296. ;;  a rather nice touch and should be preserved -- probably by
  297. ;;  providing some Lisp-level mechanism (extension to cursor-in-echo-area ?)
  298. ;;  to effect it.
  299.  
  300.  
  301. ;; Like reset_buffer in FSF's buffer.c
  302. ;;  (Except that kill-all-local-variables doesn't nuke 'permanent-local
  303. ;;   variables -- we preserve them, reset_buffer doesn't.)
  304. (defun reset-buffer (buffer)
  305.   (with-current-buffer buffer
  306.     ;(if (fboundp 'unlock-buffer) (unlock-buffer))
  307.     (kill-all-local-variables)
  308.     (setq buffer-read-only nil)
  309.     ;; don't let read only text yanked into the minibuffer
  310.     ;; permanently wedge it.
  311.     (make-local-variable 'inhibit-read-only)
  312.     (setq inhibit-read-only t)
  313.     (erase-buffer)
  314.     ;(setq default-directory nil)
  315.     (setq buffer-file-name nil)
  316.     (setq buffer-file-truename nil)
  317.     (set-buffer-modified-p nil)
  318.     (setq buffer-backed-up nil)
  319.     (setq buffer-auto-save-file-name nil)
  320.     (set-buffer-dedicated-frame buffer nil)
  321.     buffer))
  322.  
  323. (defvar minibuffer-history-variable 'minibuffer-history
  324.   "History list symbol to add minibuffer values to.
  325. Each minibuffer output is added with
  326.   (set minibuffer-history-variable
  327.        (cons STRING (symbol-value minibuffer-history-variable)))")
  328. (defvar minibuffer-history-position)
  329.  
  330. ;; Added by hniksic:
  331. (defvar initial-minibuffer-history-position)
  332. (defvar current-minibuffer-contents)
  333. (defvar current-minibuffer-point)
  334.  
  335. (defcustom minibuffer-history-minimum-string-length 3
  336.   "*If this variable is non-nil, a string will not be added to the
  337. minibuffer history if its length is less than that value."
  338.   :type '(choice (const :tag "Any" nil)
  339.          integer)
  340.   :group 'minibuffer)
  341.  
  342. (define-error 'input-error "Keyboard input error")
  343.  
  344. (put 'input-error 'display-error
  345.      #'(lambda (error-object stream)
  346.      (princ (cadr error-object) stream)))
  347.  
  348. (defun read-from-minibuffer (prompt &optional initial-contents
  349.                                     keymap
  350.                                     readp
  351.                                     history
  352.                     abbrev-table)
  353.   "Read a string from the minibuffer, prompting with string PROMPT.
  354. If optional second arg INITIAL-CONTENTS is non-nil, it is a string
  355.   to be inserted into the minibuffer before reading input.
  356.   If INITIAL-CONTENTS is (STRING . POSITION), the initial input
  357.   is STRING, but point is placed POSITION characters into the string.
  358. Third arg KEYMAP is a keymap to use whilst reading;
  359.   if omitted or nil, the default is `minibuffer-local-map'.
  360. If fourth arg READ is non-nil, then interpret the result as a lisp object
  361.   and return that object:
  362.   in other words, do `(car (read-from-string INPUT-STRING))'
  363. Fifth arg HISTORY, if non-nil, specifies a history list
  364.   and optionally the initial position in the list.
  365.   It can be a symbol, which is the history list variable to use,
  366.   or it can be a cons cell (HISTVAR . HISTPOS).
  367.   In that case, HISTVAR is the history list variable to use,
  368.   and HISTPOS is the initial position (the position in the list
  369.   which INITIAL-CONTENTS corresponds to).
  370.   If HISTORY is `t', no history will be recorded.
  371.   Positions are counted starting from 1 at the beginning of the list.
  372. Sixth arg ABBREV-TABLE, if non-nil, becomes the value of `local-abbrev-table'
  373.   in the minibuffer.
  374.  
  375. See also the variable completion-highlight-first-word-only for control over
  376.   completion display."
  377.   (if (and (not enable-recursive-minibuffers)
  378.            (> (minibuffer-depth) 0)
  379.            (eq (selected-window) (minibuffer-window)))
  380.       (error "Command attempted to use minibuffer while in minibuffer"))
  381.  
  382.   (if (and minibuffer-max-depth
  383.        (> minibuffer-max-depth 0)
  384.            (>= (minibuffer-depth) minibuffer-max-depth))
  385.       (minibuffer-max-depth-exceeded))
  386.  
  387.   ;; catch this error before the poor user has typed something...
  388.   (if history
  389.       (if (symbolp history)
  390.       (or (boundp history)
  391.           (error "History list %S is unbound" history))
  392.     (or (boundp (car history))
  393.         (error "History list %S is unbound" (car history)))))
  394.  
  395.   (if (noninteractive)
  396.       (progn
  397.         ;; XEmacs in -batch mode calls minibuffer: print the prompt.
  398.         (message "%s" (gettext prompt))
  399.         ;;#### force-output
  400.  
  401.         ;;#### Should this even be falling though to the code below?
  402.         ;;#### How does this stuff work now, anyway?
  403.         ))
  404.   (let* ((dir default-directory)
  405.          (owindow (selected-window))
  406.      (oframe (selected-frame))
  407.          (window (minibuffer-window))
  408.          (buffer (if (eq (minibuffer-depth) 0)
  409.                      (window-buffer window)
  410.            (get-buffer-create (format " *Minibuf-%d"
  411.                           (minibuffer-depth)))))
  412.          (frame (window-frame window))
  413.          (mconfig (if (eq frame (selected-frame)) 
  414.                       nil (current-window-configuration frame)))
  415.          (oconfig (current-window-configuration))
  416.      ;; dynamic scope sucks sucks sucks sucks sucks sucks.
  417.      ;; `M-x doctor' makes history a local variable, and thus
  418.      ;; our binding above is buffer-local and doesn't apply
  419.      ;; once we switch buffers!!!!  We demand better scope!
  420.      (_history_ history))
  421.     (unwind-protect
  422.          (progn
  423.            (set-buffer (reset-buffer buffer))
  424.            (setq default-directory dir)
  425.            (make-local-variable 'print-escape-newlines)
  426.            (setq print-escape-newlines t)
  427.        (make-local-variable 'current-minibuffer-contents)
  428.        (make-local-variable 'current-minibuffer-point)
  429.        (make-local-variable 'initial-minibuffer-history-position)
  430.        (setq current-minibuffer-contents ""
  431.          current-minibuffer-point 1)
  432.        (if (not minibuffer-smart-completion-tracking-behavior)
  433.            nil
  434.          (make-local-variable 'mode-motion-hook)
  435.          (or mode-motion-hook
  436.          ;;####disgusting
  437.          (setq mode-motion-hook 'minibuffer-smart-mouse-tracker))
  438.          (make-local-variable 'mouse-track-click-hook)
  439.          (add-hook 'mouse-track-click-hook
  440.                'minibuffer-smart-maybe-select-highlighted-completion))
  441.            (set-window-buffer window buffer)
  442.            (select-window window)
  443.            (set-window-hscroll window 0)
  444.            (buffer-enable-undo buffer)
  445.            (message nil)
  446.            (if initial-contents
  447.                (if (consp initial-contents)
  448.                    (progn
  449.                      (insert (car initial-contents))
  450.                      (goto-char (1+ (cdr initial-contents)))
  451.              (setq current-minibuffer-contents (car initial-contents)
  452.                current-minibuffer-point (cdr initial-contents)))
  453.          (insert initial-contents)
  454.          (setq current-minibuffer-contents initial-contents
  455.                current-minibuffer-point (point))))
  456.            (use-local-map (or keymap minibuffer-local-map))
  457.            (let ((mouse-grabbed-buffer
  458.           (and minibuffer-smart-completion-tracking-behavior
  459.                (current-buffer)))
  460.                  (current-prefix-arg current-prefix-arg)
  461.                  (help-form minibuffer-help-form)
  462.                  (minibuffer-history-variable (cond ((not _history_)
  463.                                                      'minibuffer-history)
  464.                                                     ((consp _history_)
  465.                                                      (car _history_))
  466.                                                     (t
  467.                                                      _history_)))
  468.                  (minibuffer-history-position (cond ((consp _history_)
  469.                                                      (cdr _history_))
  470.                                                     (t
  471.                                                      0)))
  472.                  (minibuffer-scroll-window owindow))
  473.          (setq initial-minibuffer-history-position
  474.            minibuffer-history-position)
  475.          (if abbrev-table
  476.          (setq local-abbrev-table abbrev-table
  477.                abbrev-mode t))
  478.          ;; This is now run from read-minibuffer-internal
  479.              ;(if minibuffer-setup-hook
  480.              ;    (run-hooks 'minibuffer-setup-hook))
  481.              ;(message nil)
  482.              (if (eq 't
  483.                      (catch 'exit
  484.                        (if (> (recursion-depth) (minibuffer-depth))
  485.                            (let ((standard-output t)
  486.                                  (standard-input t))
  487.                              (read-minibuffer-internal prompt))
  488.                            (read-minibuffer-internal prompt))))
  489.                  ;; Translate an "abort" (throw 'exit 't)
  490.                  ;;  into a real quit
  491.                  (signal 'quit '())
  492.                ;; return value
  493.                (let* ((val (progn (set-buffer buffer)
  494.                                   (if minibuffer-exit-hook
  495.                                       (run-hooks 'minibuffer-exit-hook))
  496.                                   (buffer-string)))
  497.                     (histval val)
  498.                       (err nil))
  499.                  (if readp
  500.                      (condition-case e
  501.                          (let ((v (read-from-string val)))
  502.                            (if (< (cdr v) (length val))
  503.                                (save-match-data
  504.                                  (or (string-match "[ \t\n]*\\'" val (cdr v))
  505.                                      (error "Trailing garbage following expression"))))
  506.                            (setq v (car v))
  507.                            ;; total total kludge
  508.                            (if (stringp v) (setq v (list 'quote v)))
  509.                            (setq val v))
  510.                        (end-of-file
  511.             (setq err
  512.                   '(input-error "End of input before end of expression")))
  513.                (error (setq err e))))
  514.                  ;; Add the value to the appropriate history list unless
  515.                  ;; it's already the most recent element, or it's only
  516.                  ;; two characters long.
  517.                  (if (and (symbolp minibuffer-history-variable)
  518.                           (boundp minibuffer-history-variable))
  519.              (let ((list (symbol-value minibuffer-history-variable)))
  520.                (or (eq list t)
  521.                (null val)
  522.                (and list (equal histval (car list)))
  523.                (and (stringp val)
  524.                 minibuffer-history-minimum-string-length
  525.                 (< (length val)
  526.                    minibuffer-history-minimum-string-length))
  527.                (set minibuffer-history-variable
  528.                 (if minibuffer-history-uniquify
  529.                     (cons histval (remove histval list))
  530.                   (cons histval list))))))
  531.                  (if err (signal (car err) (cdr err)))
  532.                  val))))
  533.       ;; stupid display code requires this for some reason
  534.       (set-buffer buffer)
  535.       (buffer-disable-undo buffer)
  536.       (setq buffer-read-only nil)
  537.       (erase-buffer)
  538.  
  539.       ;; restore frame configurations
  540.       (if (and mconfig (frame-live-p oframe)
  541.            (eq frame (selected-frame)))
  542.       ;; if we changed frames (due to surrogate minibuffer),
  543.       ;; and we're still on the new frame, go back to the old one.
  544.       (select-frame oframe))
  545.       (if mconfig (set-window-configuration mconfig))
  546.       (set-window-configuration oconfig))))
  547.  
  548.  
  549. (defun minibuffer-max-depth-exceeded ()
  550.   ;;
  551.   ;; This signals an error if an Nth minibuffer is invoked while N-1 are
  552.   ;; already active, whether the minibuffer window is selected or not.
  553.   ;; Since, under X, it's easy to jump out of the minibuffer (by doing M-x,
  554.   ;; getting distracted, and clicking elsewhere) many many novice users have
  555.   ;; had the problem of having multiple minibuffers build up, even to the
  556.   ;; point of exceeding max-lisp-eval-depth.  Since the variable
  557.   ;; enable-recursive-minibuffers historically/crockishly is only consulted
  558.   ;; when the minibuffer is currently active (like typing M-x M-x) it doesn't
  559.   ;; help in this situation.
  560.   ;;
  561.   ;; This routine also offers to edit .emacs for you to get rid of this
  562.   ;; complaint, like `disabled' commands do, since it's likely that non-novice
  563.   ;; users will be annoyed by this change, so we give them an easy way to get
  564.   ;; rid of it forever.
  565.   ;; 
  566.   (beep t 'minibuffer-limit-exceeded)
  567.   (message
  568.    "Minibuffer already active: abort it with `^]', enable new one with `n': ")
  569.   (let ((char (let ((cursor-in-echo-area t)) ; #### doesn't always work??
  570.         (read-char))))
  571.     (cond
  572.      ((eq char ?n)
  573.       (cond
  574.        ((y-or-n-p "Enable recursive minibuffers for other sessions too? ")
  575.     ;; This is completely disgusting, but it's basically what novice.el
  576.     ;; does.  This kind of thing should be generalized.
  577.     (setq minibuffer-max-depth nil)
  578.     (save-excursion
  579.       (set-buffer
  580.        (find-file-noselect
  581.         (substitute-in-file-name custom-file)))
  582.       (goto-char (point-min))
  583.       (if (re-search-forward 
  584.            "^(setq minibuffer-max-depth \\([0-9]+\\|'?nil\\|'?()\\))\n"
  585.            nil t)
  586.           (delete-region (match-beginning 0 ) (match-end 0))
  587.         ;; Must have been disabled by default.
  588.         (goto-char (point-max)))
  589.       (insert"\n(setq minibuffer-max-depth nil)\n")
  590.       (save-buffer))
  591.     (message "Multiple minibuffers enabled")
  592.     (sit-for 1))))
  593.      ((eq char ?)
  594.       (abort-recursive-edit))
  595.      (t
  596.       (error "Minibuffer already active")))))
  597.  
  598.  
  599. ;;;; Guts of minibuffer completion
  600.  
  601.  
  602. ;; Used by minibuffer-do-completion
  603. (defvar last-exact-completion)
  604.  
  605. (defun temp-minibuffer-message (m)
  606.   (let ((savemax (point-max)))
  607.     (save-excursion
  608.       (goto-char (point-max))
  609.       (message nil)
  610.       (insert m))
  611.     (let ((inhibit-quit t))
  612.       (sit-for 2)
  613.       (delete-region savemax (point-max))
  614.       ;;  If the user types a ^G while we're in sit-for, then quit-flag 
  615.       ;;  gets set. In this case, we want that ^G to be interpreted 
  616.       ;;  as a normal character, and act just like typeahead.
  617.       (if (and quit-flag (not unread-command-event))
  618.           (setq unread-command-event (character-to-event (quit-char))
  619.                 quit-flag nil)))))
  620.  
  621.  
  622. ;; Determines whether buffer-string is an exact completion
  623. (defun exact-minibuffer-completion-p (buffer-string)
  624.   (cond ((not minibuffer-completion-table)
  625.          ;; Empty alist
  626.          nil)
  627.         ((vectorp minibuffer-completion-table)
  628.          (let ((tem (intern-soft buffer-string
  629.                                  minibuffer-completion-table)))
  630.            (if (or tem
  631.                    (and (string-equal buffer-string "nil")
  632.                         ;; intern-soft loses for 'nil
  633.                         (catch 'found
  634.                           (mapatoms #'(lambda (s)
  635.                     (if (string-equal
  636.                          (symbol-name s)
  637.                          buffer-string)
  638.                         (throw 'found t)))
  639.                     minibuffer-completion-table)
  640.                           nil)))
  641.                (if minibuffer-completion-predicate
  642.                    (funcall minibuffer-completion-predicate
  643.                             tem)
  644.                    t)
  645.                nil)))
  646.         ((and (consp minibuffer-completion-table)
  647.               ;;#### Emacs-Lisp truly sucks!
  648.               ;; lambda, autoload, etc
  649.               (not (symbolp (car minibuffer-completion-table))))
  650.          (if (not completion-ignore-case)
  651.              (assoc buffer-string minibuffer-completion-table)
  652.              (let ((s (upcase buffer-string))
  653.                    (tail minibuffer-completion-table)
  654.                    tem)
  655.                (while tail
  656.                  (setq tem (car (car tail)))
  657.                  (if (or (equal tem buffer-string)
  658.                          (equal tem s)
  659.                          (equal (upcase tem) s))
  660.                      (setq s 'win
  661.                            tail nil)    ;exit
  662.                      (setq tail (cdr tail))))
  663.                (eq s 'win))))
  664.         (t
  665.          (funcall minibuffer-completion-table
  666.                   buffer-string
  667.                   minibuffer-completion-predicate
  668.                   'lambda)))
  669.   )
  670.  
  671. ;; 0 'none                 no possible completion
  672. ;; 1 'unique               was already an exact and unique completion
  673. ;; 3 'exact                was already an exact (but nonunique) completion
  674. ;; NOT USED 'completed-exact-unique completed to an exact and completion 
  675. ;; 4 'completed-exact      completed to an exact (but nonunique) completion
  676. ;; 5 'completed            some completion happened
  677. ;; 6 'uncompleted          no completion happened
  678. (defun minibuffer-do-completion-1 (buffer-string completion)
  679.   (cond ((not completion)
  680.          'none)
  681.         ((eq completion t)
  682.          ;; exact and unique match
  683.          'unique)
  684.         (t
  685.          ;; It did find a match.  Do we match some possibility exactly now?
  686.          (let ((completedp (not (string-equal completion buffer-string))))
  687.            (if completedp
  688.                (progn
  689.                  ;; Some completion happened
  690.                  (erase-buffer)
  691.                  (insert completion)
  692.                  (setq buffer-string completion)))
  693.            (if (exact-minibuffer-completion-p buffer-string)
  694.                ;; An exact completion was possible
  695.                (if completedp
  696. ;; Since no callers need to know the difference, don't bother
  697. ;;  with this (potentially expensive) discrimination.
  698. ;;                 (if (eq (try-completion completion
  699. ;;                                         minibuffer-completion-table
  700. ;;                                         minibuffer-completion-predicate)
  701. ;;                         't)
  702. ;;                     'completed-exact-unique
  703.                        'completed-exact
  704. ;;                     )
  705.                    'exact)
  706.                ;; Not an exact match
  707.                (if completedp
  708.                    'completed
  709.                    'uncompleted))))))
  710.  
  711.  
  712. (defun minibuffer-do-completion (buffer-string)
  713.   (let* ((completion (try-completion buffer-string
  714.                                      minibuffer-completion-table
  715.                                      minibuffer-completion-predicate))
  716.          (status (minibuffer-do-completion-1 buffer-string completion))
  717.          (last last-exact-completion))
  718.     (setq last-exact-completion nil)
  719.     (cond ((eq status 'none)
  720.            ;; No completions
  721.            (ding nil 'no-completion)
  722.            (temp-minibuffer-message " [No match]"))
  723.           ((eq status 'unique)
  724.            )
  725.           (t
  726.            ;; It did find a match.  Do we match some possibility exactly now?
  727.            (if (not (string-equal completion buffer-string))
  728.                (progn
  729.                  ;; Some completion happened
  730.                  (erase-buffer)
  731.                  (insert completion)
  732.                  (setq buffer-string completion)))
  733.            (cond ((eq status 'exact)
  734.                   ;; If the last exact completion and this one were
  735.                   ;;  the same, it means we've already given a
  736.                   ;;  "Complete but not unique" message and that the
  737.                   ;;  user's hit TAB again, so now we give help.
  738.                   (setq last-exact-completion completion)
  739.                   (if (equal buffer-string last)
  740.                       (minibuffer-completion-help)))
  741.                  ((eq status 'uncompleted)
  742.                   (if completion-auto-help
  743.                       (minibuffer-completion-help)
  744.                       (temp-minibuffer-message " [Next char not unique]")))
  745.                  (t
  746.                   nil))))
  747.     status))
  748.  
  749.  
  750. ;;;; completing-read
  751.  
  752. (defun completing-read (prompt table
  753.                         &optional predicate require-match
  754.                                   initial-contents history)
  755.   "Read a string in the minibuffer, with completion.
  756. Args: PROMPT, TABLE, PREDICATE, REQUIRE-MATCH, INITIAL-CONTENTS, HISTORY.
  757. PROMPT is a string to prompt with; normally it ends in a colon and a space.
  758. TABLE is an alist whose elements' cars are strings, or an obarray.
  759. PREDICATE limits completion to a subset of TABLE.
  760. See `try-completion' for more details on completion, TABLE, and PREDICATE.
  761. If REQUIRE-MATCH is non-nil, the user is not allowed to exit unless
  762.  the input is (or completes to) an element of TABLE or is null.
  763.  If it is also not t, Return does not exit if it does non-null completion.
  764. If INITIAL-CONTENTS is non-nil, insert it in the minibuffer initially.
  765.   If it is (STRING . POSITION), the initial input
  766.   is STRING, but point is placed POSITION characters into the string.
  767. HISTORY, if non-nil, specifies a history list
  768.   and optionally the initial position in the list.
  769.   It can be a symbol, which is the history list variable to use,
  770.   or it can be a cons cell (HISTVAR . HISTPOS).
  771.   In that case, HISTVAR is the history list variable to use,
  772.   and HISTPOS is the initial position (the position in the list
  773.   which INITIAL-CONTENTS corresponds to).
  774.   If HISTORY is `t', no history will be recorded.
  775.   Positions are counted starting from 1 at the beginning of the list.
  776. Completion ignores case if the ambient value of
  777.   `completion-ignore-case' is non-nil."
  778.   (let ((minibuffer-completion-table table)
  779.         (minibuffer-completion-predicate predicate)
  780.         (minibuffer-completion-confirm (if (eq require-match 't) nil t))
  781.         (last-exact-completion nil))
  782.     (read-from-minibuffer prompt
  783.                           initial-contents
  784.                           (if (not require-match)
  785.                               minibuffer-local-completion-map
  786.                               minibuffer-local-must-match-map)
  787.                           nil
  788.                           history)))
  789.  
  790.  
  791. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  792. ;;;;                   Minibuffer completion commands                   ;;;;
  793. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  794.  
  795.  
  796. (defun minibuffer-complete ()
  797.   "Complete the minibuffer contents as far as possible.
  798. Return nil if there is no valid completion, else t.
  799. If no characters can be completed, display a list of possible completions.
  800. If you repeat this command after it displayed such a list,
  801. scroll the window of possible completions."
  802.   (interactive)
  803.   ;; If the previous command was not this, then mark the completion
  804.   ;;  buffer obsolete.
  805.   (or (eq last-command this-command)
  806.       (setq minibuffer-scroll-window nil))
  807.   (let ((window minibuffer-scroll-window))
  808.     (if (and window (windowp window) (window-buffer window)
  809.              (buffer-name (window-buffer window)))
  810.     ;; If there's a fresh completion window with a live buffer
  811.     ;;  and this command is repeated, scroll that window.
  812.     (let ((obuf (current-buffer)))
  813.           (unwind-protect
  814.           (progn
  815.         (set-buffer (window-buffer window))
  816.         (if (pos-visible-in-window-p (point-max) window)
  817.             ;; If end is in view, scroll up to the beginning.
  818.             (set-window-start window (point-min))
  819.           ;; Else scroll down one frame.
  820.           (scroll-other-window)))
  821.         (set-buffer obuf))
  822.           nil)
  823.       (let ((status (minibuffer-do-completion (buffer-string))))
  824.     (if (eq status 'none)
  825.         nil
  826.       (progn
  827.         (cond ((eq status 'unique)
  828.            (temp-minibuffer-message
  829.             " [Sole completion]"))
  830.           ((eq status 'exact)
  831.            (temp-minibuffer-message
  832.             " [Complete, but not unique]")))
  833.         t))))))
  834.  
  835.  
  836. (defun minibuffer-complete-and-exit ()
  837.   "Complete the minibuffer contents, and maybe exit.
  838. Exit if the name is valid with no completion needed.
  839. If name was completed to a valid match,
  840. a repetition of this command will exit."
  841.   (interactive)
  842.   (if (= (point-min) (point-max))
  843.       ;; Crockishly allow user to specify null string
  844.       (throw 'exit nil))
  845.   (let ((buffer-string (buffer-string)))
  846.     ;; Short-cut -- don't call minibuffer-do-completion if we already
  847.     ;;  have an (possibly nonunique) exact completion.
  848.     (if (exact-minibuffer-completion-p buffer-string)
  849.         (throw 'exit nil))
  850.     (let ((status (minibuffer-do-completion buffer-string)))
  851.       (if (or (eq status 'unique)
  852.               (eq status 'exact)
  853.               (if (or (eq status 'completed-exact)
  854.                       (eq status 'completed-exact-unique))
  855.                   (if minibuffer-completion-confirm
  856.                       (progn (temp-minibuffer-message " [Confirm]")
  857.                              nil)
  858.                       t)))
  859.           (throw 'exit nil)))))
  860.  
  861.  
  862. (defun self-insert-and-exit ()
  863.   "Terminate minibuffer input."
  864.   (interactive)
  865.   (self-insert-command 1)
  866.   (throw 'exit nil))
  867.  
  868. (defun exit-minibuffer ()
  869.   "Terminate this minibuffer argument.
  870. If minibuffer-confirm-incomplete is true, and we are in a completing-read
  871. of some kind, and the contents of the minibuffer is not an existing
  872. completion, requires an additional RET before the minibuffer will be exited
  873. \(assuming that RET was the character that invoked this command:
  874. the character in question must be typed again)."
  875.   (interactive)
  876.   (if (not minibuffer-confirm-incomplete)
  877.       (throw 'exit nil))
  878.   (let ((buffer-string (buffer-string)))
  879.     (if (exact-minibuffer-completion-p buffer-string)
  880.         (throw 'exit nil))
  881.     (let ((completion (if (not minibuffer-completion-table)
  882.                           t
  883.                           (try-completion buffer-string
  884.                                           minibuffer-completion-table
  885.                                           minibuffer-completion-predicate))))
  886.       (if (or (eq completion 't)
  887.               ;; Crockishly allow user to specify null string
  888.               (string-equal buffer-string ""))
  889.           (throw 'exit nil))
  890.       (if completion ;; rewritten for I18N3 snarfing
  891.       (temp-minibuffer-message " [incomplete; confirm]")
  892.     (temp-minibuffer-message " [no completions; confirm]"))
  893.       (let ((event (let ((inhibit-quit t))
  894.              (prog1
  895.              (next-command-event)
  896.                (setq quit-flag nil)))))
  897.         (cond ((equal event last-command-event)
  898.                (throw 'exit nil))
  899.               ((equal (quit-char) (event-to-character event))
  900.                ;; Minibuffer abort.
  901.                (throw 'exit t)))
  902.         (dispatch-event event)))))
  903.  
  904. ;;;; minibuffer-complete-word
  905.  
  906.  
  907. ;;;#### I think I have done this correctly; it certainly is simpler
  908. ;;;#### than what the C code seemed to be trying to do.
  909. (defun minibuffer-complete-word ()
  910.   "Complete the minibuffer contents at most a single word.
  911. After one word is completed as much as possible, a space or hyphen
  912. is added, provided that matches some possible completion.
  913. Return nil if there is no valid completion, else t."
  914.   (interactive)
  915.   (let* ((buffer-string (buffer-string))
  916.          (completion (try-completion buffer-string
  917.                                      minibuffer-completion-table
  918.                                      minibuffer-completion-predicate))
  919.          (status (minibuffer-do-completion-1 buffer-string completion)))
  920.     (cond ((eq status 'none)
  921.            (ding nil 'no-completion)
  922.            (temp-minibuffer-message " [No match]")
  923.            nil)
  924.           ((eq status 'unique)
  925.            ;; New message, only in this new Lisp code
  926.            (temp-minibuffer-message " [Sole completion]")
  927.            t)
  928.           (t
  929.            (cond ((or (eq status 'uncompleted)
  930.                       (eq status 'exact))
  931.                   (let ((foo #'(lambda (s)
  932.                  (condition-case nil
  933.                      (if (try-completion
  934.                       (concat buffer-string s)
  935.                       minibuffer-completion-table
  936.                       minibuffer-completion-predicate)
  937.                      (progn
  938.                        (goto-char (point-max))
  939.                        (insert s)
  940.                        t)
  941.                                        nil)
  942.                                    (error nil))))
  943.                         (char last-command-char))
  944.                     ;; Try to complete by adding a word-delimiter
  945.                     (or (and (characterp char) (> char 0)
  946.                              (funcall foo (char-to-string char)))
  947.                         (and (not (eq char ?\ ))
  948.                              (funcall foo " "))
  949.                         (and (not (eq char ?\-))
  950.                              (funcall foo "-"))
  951.                         (progn
  952.                           (if completion-auto-help 
  953.                               (minibuffer-completion-help)
  954.                               ;; New message, only in this new Lisp code
  955.                 ;; rewritten for I18N3 snarfing
  956.                 (if (eq status 'exact)
  957.                 (temp-minibuffer-message
  958.                  " [Complete, but not unique]")
  959.                   (temp-minibuffer-message " [Ambiguous]")))
  960.                           nil))))
  961.                  (t
  962.                   (erase-buffer)
  963.                   (insert completion)
  964.                   ;; First word-break in stuff found by completion
  965.                   (goto-char (point-min))
  966.                   (let ((len (length buffer-string))
  967.                         n)
  968.                     (if (and (< len (length completion))
  969.                              (catch 'match
  970.                                (setq n 0)
  971.                                (while (< n len)
  972.                                  (if (char-equal
  973.                                        (upcase (aref buffer-string n))
  974.                                        (upcase (aref completion n)))
  975.                                      (setq n (1+ n))
  976.                                      (throw 'match nil)))
  977.                                t)
  978.                              (progn
  979.                                (goto-char (point-min))
  980.                                (forward-char len)
  981.                                (re-search-forward "\\W" nil t)))
  982.                         (delete-region (point) (point-max))
  983.                         (goto-char (point-max))))
  984.                   t))))))
  985.  
  986.  
  987. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  988. ;;;;                      "Smart minibuffer" hackery                    ;;;;
  989. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  990.  
  991. ;;; ("Kludgy minibuffer hackery" is perhaps a better name)
  992.  
  993. ;; This works by setting `mouse-grabbed-buffer' to the minibuffer,
  994. ;; defining button2 in the minibuffer keymap to
  995. ;; `minibuffer-smart-select-highlighted-completion', and setting the
  996. ;; mode-motion-hook of the minibuffer to `minibuffer-mouse-tracker'.
  997. ;; By setting `mouse-grabbed-buffer', the minibuffer's keymap and
  998. ;; mode-motion-hook apply (for mouse motion and presses) no matter
  999. ;; what buffer the mouse is over.  Then, `minibuffer-mouse-tracker'
  1000. ;; examines the text under the mouse looking for something that looks
  1001. ;; like a completion, and causes it to be highlighted, and
  1002. ;; `minibuffer-smart-select-highlighted-completion' looks for a
  1003. ;; flagged completion under the mouse and inserts it.  This has the
  1004. ;; following advantages:
  1005. ;;
  1006. ;; -- filenames and such in any buffer can be inserted by clicking,
  1007. ;;    not just completions
  1008. ;;
  1009. ;; but the following disadvantages:
  1010. ;;
  1011. ;; -- unless you're aware of the "filename in any buffer" feature,
  1012. ;;    the fact that strings in arbitrary buffers get highlighted appears
  1013. ;;    as a bug
  1014. ;; -- mouse motion can cause ange-ftp actions -- bad bad bad.
  1015. ;;
  1016. ;; There's some hackery in minibuffer-mouse-tracker to try to avoid the
  1017. ;; ange-ftp stuff, but it doesn't work.
  1018. ;;
  1019.  
  1020. (defcustom minibuffer-smart-completion-tracking-behavior nil
  1021.   "*If non-nil, look for completions under mouse in all buffers.
  1022. This allows you to click on something that looks like a completion
  1023. and have it selected, regardless of what buffer it is in.
  1024.  
  1025. This is not enabled by default because
  1026.  
  1027. -- The \"mysterious\" highlighting in normal buffers is confusing to
  1028.    people not expecting it, and looks like a bug
  1029. -- If ange-ftp is enabled, this tracking sometimes causes ange-ftp
  1030.    action as a result of mouse motion, which is *bad bad bad*.
  1031.    Hopefully this bug will be fixed at some point."
  1032.   :type 'boolean
  1033.   :group 'minibuffer)
  1034.  
  1035. (defun minibuffer-smart-mouse-tracker (event)
  1036.   ;; Used as the mode-motion-hook of the minibuffer window, which is the
  1037.   ;; value of `mouse-grabbed-buffer' while the minibuffer is active.  If
  1038.   ;; the word under the mouse is a valid minibuffer completion, then it
  1039.   ;; is highlighted.
  1040.   ;;
  1041.   ;; We do some special voodoo when we're reading a pathname, because
  1042.   ;; the way filename completion works is funny.  Possibly there's some
  1043.   ;; more general way this could be dealt with...
  1044.   ;;
  1045.   ;; We do some further voodoo when reading a pathname that is an
  1046.   ;; ange-ftp or efs path, because causing FTP activity as a result of
  1047.   ;; mouse motion is a really bad time.
  1048.   ;;
  1049.   (and minibuffer-smart-completion-tracking-behavior
  1050.        (event-point event)
  1051.        ;; avoid conflict with display-completion-list extents
  1052.        (not (extent-at (event-point event)
  1053.                (event-buffer event)
  1054.                'list-mode-item))
  1055.        (let ((filename-kludge-p (eq minibuffer-completion-table
  1056.                     'read-file-name-internal)))
  1057.      (mode-motion-highlight-internal
  1058.       event
  1059.       #'(lambda () (default-mouse-track-beginning-of-word
  1060.              (if filename-kludge-p 'nonwhite t)))
  1061.       #'(lambda ()
  1062.           (let ((p (point))
  1063.             (string ""))
  1064.         (default-mouse-track-end-of-word
  1065.           (if filename-kludge-p 'nonwhite t))
  1066.         (if (and (/= p (point)) minibuffer-completion-table)
  1067.             (setq string (buffer-substring p (point))))
  1068.         (if (string-match "\\`[ \t\n]*\\'" string)
  1069.             (goto-char p)
  1070.           (if filename-kludge-p
  1071.               (setq string (minibuffer-smart-select-kludge-filename
  1072.                     string)))
  1073.           ;; try-completion bogusly returns a string even when
  1074.           ;; that string is complete if that string is also a
  1075.           ;; prefix for other completions.  This means that we
  1076.           ;; can't just do the obvious thing, (eq t
  1077.           ;; (try-completion ...)).
  1078.           (let (comp)
  1079.             (if (and filename-kludge-p
  1080.                  ;; #### evil evil evil evil
  1081.                  (or (and (fboundp 'ange-ftp-ftp-path)
  1082.                       (ange-ftp-ftp-path string))
  1083.                  (and (fboundp 'efs-ftp-path)
  1084.                       (efs-ftp-path string))))
  1085.             (setq comp t)
  1086.               (setq comp
  1087.                 (try-completion string
  1088.                         minibuffer-completion-table
  1089.                         minibuffer-completion-predicate)))
  1090.             (or (eq comp t)
  1091.             (and (equal comp string)
  1092.                  (or (null minibuffer-completion-predicate)
  1093.                  (stringp
  1094.                   minibuffer-completion-predicate) ; ???
  1095.                  (funcall minibuffer-completion-predicate
  1096.                       (if (vectorp
  1097.                            minibuffer-completion-table)
  1098.                           (intern-soft
  1099.                            string
  1100.                            minibuffer-completion-table)
  1101.                         string))))
  1102.             (goto-char p))))))))))
  1103.  
  1104. (defun minibuffer-smart-select-kludge-filename (string)
  1105.   (save-excursion
  1106.     (set-buffer mouse-grabbed-buffer) ; the minibuf
  1107.     (let ((kludge-string (concat (buffer-string) string)))
  1108.       (if (or (and (fboundp 'ange-ftp-ftp-path)
  1109.            (ange-ftp-ftp-path kludge-string))
  1110.            (and (fboundp 'efs-ftp-path) (efs-ftp-path kludge-string)))
  1111.        ;; #### evil evil evil, but more so.
  1112.        string
  1113.      (append-expand-filename (buffer-string) string)))))
  1114.  
  1115. (defun minibuffer-smart-select-highlighted-completion (event)
  1116.   "Select the highlighted text under the mouse as a minibuffer response.
  1117. When the minibuffer is being used to prompt the user for a completion,
  1118. any valid completions which are visible on the frame will highlight
  1119. when the mouse moves over them.  Clicking \\<minibuffer-local-map>\
  1120. \\[minibuffer-smart-select-highlighted-completion] will select the
  1121. highlighted completion under the mouse.
  1122.  
  1123. If the mouse is clicked while not over a highlighted completion,
  1124. then the global binding of \\[minibuffer-smart-select-highlighted-completion] \
  1125. will be executed instead.  In this\nway you can get at the normal global \
  1126. behavior of \\[minibuffer-smart-select-highlighted-completion] as well as
  1127. the special minibuffer behavior."
  1128.   (interactive "e")
  1129.   (if minibuffer-smart-completion-tracking-behavior
  1130.       (minibuffer-smart-select-highlighted-completion-1 event t)
  1131.     (let ((command (lookup-key global-map
  1132.                    (vector current-mouse-event))))
  1133.       (if command (call-interactively command)))))
  1134.  
  1135. (defun minibuffer-smart-select-highlighted-completion-1 (event global-p)
  1136.   (let* ((filename-kludge-p (eq minibuffer-completion-table
  1137.                 'read-file-name-internal))
  1138.      completion
  1139.      command-p
  1140.      (evpoint (event-point event))
  1141.      (evextent (and evpoint (extent-at evpoint (event-buffer event)
  1142.                        'list-mode-item))))
  1143.     (if evextent
  1144.     ;; avoid conflict with display-completion-list extents.
  1145.     ;; if we find one, do that behavior instead.
  1146.     (list-mode-item-selected-1 evextent event)
  1147.       (save-excursion
  1148.     (let* ((buffer (window-buffer (event-window event)))
  1149.            (p (event-point event))
  1150.            (extent (and p (extent-at p buffer 'mouse-face))))
  1151.       (set-buffer buffer)
  1152.       (if (not (and (extent-live-p extent)
  1153.             (eq (extent-object extent) (current-buffer))
  1154.             (not (extent-detached-p extent))))
  1155.           (setq command-p t)
  1156.         ;; ...else user has selected a highlighted completion.
  1157.         (setq completion
  1158.           (buffer-substring (extent-start-position extent)
  1159.                     (extent-end-position extent)))
  1160.         (if filename-kludge-p
  1161.         (setq completion (minibuffer-smart-select-kludge-filename
  1162.                   completion)))
  1163.         ;; remove the extent so that it's not hanging around in
  1164.         ;; *Completions*
  1165.         (detach-extent extent)
  1166.         (set-buffer mouse-grabbed-buffer)
  1167.         (erase-buffer)
  1168.         (insert completion))))
  1169.       ;; we need to execute the command or do the throw outside of the
  1170.       ;; save-excursion.
  1171.       (cond ((and command-p global-p)
  1172.          (let ((command (lookup-key global-map
  1173.                     (vector current-mouse-event))))
  1174.            (if command
  1175.            (call-interactively command)
  1176.          (if minibuffer-completion-table
  1177.              (error
  1178.               "Highlighted words are valid completions.  You may select one.")
  1179.            (error "no completions")))))
  1180.         ((not command-p)
  1181.          ;; things get confused if the minibuffer is terminated while
  1182.          ;; not selected.
  1183.          (select-window (minibuffer-window))
  1184.          (if (and filename-kludge-p (file-directory-p completion))
  1185.          ;; if the user clicked middle on a directory name, display the
  1186.          ;; files in that directory.
  1187.          (progn
  1188.            (goto-char (point-max))
  1189.            (minibuffer-completion-help))
  1190.            ;; otherwise, terminate input
  1191.            (throw 'exit nil)))))))
  1192.  
  1193. (defun minibuffer-smart-maybe-select-highlighted-completion
  1194.   (event &optional click-count)
  1195.   "Like minibuffer-smart-select-highlighted-completion but does nothing if
  1196. there is no completion (as opposed to executing the global binding).  Useful
  1197. as the value of `mouse-track-click-hook'."
  1198.   (interactive "e")
  1199.   (minibuffer-smart-select-highlighted-completion-1 event nil))
  1200.  
  1201. (define-key minibuffer-local-map 'button2
  1202.   'minibuffer-smart-select-highlighted-completion)
  1203.  
  1204.  
  1205. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1206. ;;;;                         Minibuffer History                         ;;;;
  1207. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1208.  
  1209. (defvar minibuffer-history '()
  1210.   "Default minibuffer history list.
  1211. This is used for all minibuffer input except when an alternate history
  1212. list is specified.")
  1213.  
  1214. ;; Some other history lists:
  1215. ;;
  1216. (defvar minibuffer-history-search-history '())
  1217. (defvar function-history '())
  1218. (defvar variable-history '())
  1219. (defvar buffer-history '())
  1220. (defvar shell-command-history '())
  1221. (defvar file-name-history '())
  1222.  
  1223. (defvar read-expression-history nil)
  1224.  
  1225. (defvar minibuffer-history-sexp-flag nil ;weird FSF Emacs kludge
  1226.   "Non-nil when doing history operations on `command-history'.
  1227. More generally, indicates that the history list being acted on
  1228. contains expressions rather than strings.")
  1229.  
  1230. (defun previous-matching-history-element (regexp n)
  1231.   "Find the previous history element that matches REGEXP.
  1232. \(Previous history elements refer to earlier actions.)
  1233. With prefix argument N, search for Nth previous match.
  1234. If N is negative, find the next or Nth next match."
  1235.   (interactive
  1236.    (let ((enable-recursive-minibuffers t)
  1237.      (minibuffer-history-sexp-flag nil))
  1238.      (if (eq 't (symbol-value minibuffer-history-variable))
  1239.      (error "History is not being recorded in this context"))
  1240.      (list (read-from-minibuffer "Previous element matching (regexp): "
  1241.                  (car minibuffer-history-search-history)
  1242.                  minibuffer-local-map
  1243.                  nil
  1244.                  'minibuffer-history-search-history)
  1245.        (prefix-numeric-value current-prefix-arg))))
  1246.   (let ((history (symbol-value minibuffer-history-variable))
  1247.     prevpos
  1248.     (pos minibuffer-history-position))
  1249.     (if (eq history t)
  1250.     (error "History is not being recorded in this context"))
  1251.     (while (/= n 0)
  1252.       (setq prevpos pos)
  1253.       (setq pos (min (max 1 (+ pos (if (< n 0) -1 1))) (length history)))
  1254.       (if (= pos prevpos)
  1255.       (if (= pos 1) ;; rewritten for I18N3 snarfing
  1256.           (error "No later matching history item")
  1257.         (error "No earlier matching history item")))
  1258.       (if (string-match regexp
  1259.             (if minibuffer-history-sexp-flag
  1260.                 (let ((print-level nil))
  1261.                   (prin1-to-string (nth (1- pos) history)))
  1262.                             (nth (1- pos) history)))
  1263.       (setq n (+ n (if (< n 0) 1 -1)))))
  1264.     (setq minibuffer-history-position pos)
  1265.     (setq current-minibuffer-contents (buffer-string)
  1266.       current-minibuffer-point (point))
  1267.     (erase-buffer)
  1268.     (let ((elt (nth (1- pos) history)))
  1269.       (insert (if minibuffer-history-sexp-flag
  1270.           (let ((print-level nil))
  1271.             (prin1-to-string elt))
  1272.                   elt)))
  1273.       (goto-char (point-min)))
  1274.   (if (or (eq (car (car command-history)) 'previous-matching-history-element)
  1275.       (eq (car (car command-history)) 'next-matching-history-element))
  1276.       (setq command-history (cdr command-history))))
  1277.  
  1278. (defun next-matching-history-element (regexp n)
  1279.   "Find the next history element that matches REGEXP.
  1280. \(The next history element refers to a more recent action.)
  1281. With prefix argument N, search for Nth next match.
  1282. If N is negative, find the previous or Nth previous match."
  1283.   (interactive
  1284.    (let ((enable-recursive-minibuffers t)
  1285.      (minibuffer-history-sexp-flag nil))
  1286.      (if (eq t (symbol-value minibuffer-history-variable))
  1287.      (error "History is not being recorded in this context"))
  1288.      (list (read-from-minibuffer "Next element matching (regexp): "
  1289.                  (car minibuffer-history-search-history)
  1290.                  minibuffer-local-map
  1291.                  nil
  1292.                  'minibuffer-history-search-history)
  1293.        (prefix-numeric-value current-prefix-arg))))
  1294.   (previous-matching-history-element regexp (- n)))
  1295.  
  1296. (defun next-history-element (n)
  1297.   "Insert the next element of the minibuffer history into the minibuffer."
  1298.   (interactive "p")
  1299.   (if (eq 't (symbol-value minibuffer-history-variable))
  1300.       (error "History is not being recorded in this context"))
  1301.   (unless (zerop n)
  1302.     (when (eq minibuffer-history-position
  1303.           initial-minibuffer-history-position)
  1304.       (setq current-minibuffer-contents (buffer-string)
  1305.         current-minibuffer-point (point)))
  1306.     (let ((narg (- minibuffer-history-position n)))
  1307.       (cond ((< narg 0)
  1308.          (error "No following item in %s" minibuffer-history-variable))
  1309.         ((> narg (length (symbol-value minibuffer-history-variable)))
  1310.          (error "No preceding item in %s" minibuffer-history-variable)))
  1311.       (erase-buffer)
  1312.       (setq minibuffer-history-position narg)
  1313.       (if (eq narg initial-minibuffer-history-position)
  1314.       (progn
  1315.         (insert current-minibuffer-contents)
  1316.         (goto-char current-minibuffer-point))
  1317.     (let ((elt (nth (1- minibuffer-history-position)
  1318.             (symbol-value minibuffer-history-variable))))
  1319.       (insert
  1320.        (if (not (stringp elt))
  1321.            (let ((print-level nil))
  1322.          (condition-case nil
  1323.              (let ((print-readably t)
  1324.                (print-escape-newlines t))
  1325.                (prin1-to-string elt))
  1326.            (error (prin1-to-string elt))))
  1327.          elt)))
  1328.     ;; FSF has point-min here.
  1329.     (goto-char (point-max))))))
  1330.  
  1331. (defun previous-history-element (n)
  1332.   "Inserts the previous element of the minibuffer history into the minibuffer."
  1333.   (interactive "p")
  1334.   (next-history-element (- n)))
  1335.  
  1336. (defun next-complete-history-element (n)
  1337.   "Get next element of history which is a completion of minibuffer contents."
  1338.   (interactive "p")
  1339.   (let ((point-at-start (point)))
  1340.     (next-matching-history-element
  1341.      (concat "^" (regexp-quote (buffer-substring (point-min) (point)))) n)
  1342.     ;; next-matching-history-element always puts us at (point-min).
  1343.     ;; Move to the position we were at before changing the buffer contents.
  1344.     ;; This is still sensical, because the text before point has not changed.
  1345.     (goto-char point-at-start)))
  1346.  
  1347. (defun previous-complete-history-element (n)
  1348.   "Get previous element of history which is a completion of minibuffer contents."
  1349.   (interactive "p")
  1350.   (next-complete-history-element (- n)))
  1351.  
  1352.  
  1353. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1354. ;;;;                reading various things from a minibuffer            ;;;;
  1355. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1356.  
  1357. (defun read-expression (prompt &optional initial-contents history)
  1358.   "Return a Lisp object read using the minibuffer.
  1359. Prompt with PROMPT.  If non-nil, optional second arg INITIAL-CONTENTS
  1360. is a string to insert in the minibuffer before reading.
  1361. Third arg HISTORY, if non-nil, specifies a history list."
  1362.   (let ((minibuffer-history-sexp-flag t)
  1363.     ;; Semi-kludge to get around M-x C-x o M-ESC trying to do completion.
  1364.     (minibuffer-completion-table nil))
  1365.     (read-from-minibuffer prompt
  1366.               initial-contents
  1367.               read-expression-map
  1368.               t
  1369.               (or history 'read-expression-history)
  1370.               lisp-mode-abbrev-table)))
  1371.  
  1372. (defun read-string (prompt &optional initial-contents history)
  1373.   "Return a string from the minibuffer, prompting with string PROMPT.
  1374. If non-nil, optional second arg INITIAL-CONTENTS is a string to insert
  1375. in the minibuffer before reading.
  1376. Third arg HISTORY, if non-nil, specifies a history list."
  1377.   (let ((minibuffer-completion-table nil))
  1378.     (read-from-minibuffer prompt
  1379.               initial-contents
  1380.               minibuffer-local-map
  1381.               nil history)))
  1382.  
  1383. (defun eval-minibuffer (prompt &optional initial-contents history)
  1384.   "Return value of Lisp expression read using the minibuffer.
  1385. Prompt with PROMPT.  If non-nil, optional second arg INITIAL-CONTENTS
  1386. is a string to insert in the minibuffer before reading.
  1387. Third arg HISTORY, if non-nil, specifies a history list."
  1388.   (eval (read-expression prompt initial-contents history)))
  1389.  
  1390. ;;;#### Screw this crock!!
  1391. ;(defun read-no-blanks-input (prompt &optional initial-contents)
  1392. ; "Read a string from the terminal, not allowing blanks.
  1393. ;Prompt with PROMPT.  If non-nil, optional second arg INITIAL-CONTENTS
  1394. ;is a string to insert in the minibuffer before reading."
  1395. ;  (let ((minibuffer-completion-table nil))
  1396. ; (read-from-minibuffer prompt
  1397. ;                       initial-contents
  1398. ;                       minibuffer-local-ns-map
  1399. ;                       nil)))
  1400.  
  1401. ;; The name `command-history' is already taken
  1402. (defvar read-command-history '())
  1403.  
  1404. (defun read-command (prompt)
  1405.   "Read the name of a command and return as a symbol.
  1406. Prompts with PROMPT."
  1407.   (intern (completing-read prompt obarray 'commandp t nil
  1408.                ;; 'command-history is not right here: that's a
  1409.                ;; list of evalable forms, not a history list.
  1410.                'read-command-history
  1411.                )))
  1412.  
  1413. (defun read-function (prompt)
  1414.   "Read the name of a function and return as a symbol.
  1415. Prompts with PROMPT."
  1416.   (intern (completing-read prompt obarray 'fboundp t nil
  1417.                'function-history)))
  1418.  
  1419. (defun read-variable (prompt)
  1420.   "Read the name of a user variable and return it as a symbol.
  1421. Prompts with PROMPT.
  1422. A user variable is one whose documentation starts with a `*' character."
  1423.   (intern (completing-read prompt obarray 'user-variable-p t nil
  1424.                'variable-history)))
  1425.  
  1426. (defun read-buffer (prompt &optional default require-match)
  1427.   "Read the name of a buffer and return as a string.
  1428. Prompts with PROMPT.  Optional second arg DEFAULT is value to return if user
  1429. enters an empty line.  If optional third arg REQUIRE-MATCH is non-nil,
  1430. only existing buffer names are allowed."
  1431.   (let ((prompt (if default 
  1432.                     (format "%s(default %s) "
  1433.                             (gettext prompt) (if (bufferp default)
  1434.                          (buffer-name default)
  1435.                            default))
  1436.                     prompt))
  1437.         (alist (mapcar #'(lambda (b) (cons (buffer-name b) b))
  1438.                        (buffer-list)))
  1439.         result)
  1440.     (while (progn
  1441.              (setq result (completing-read prompt alist nil require-match
  1442.                        nil 'buffer-history))
  1443.              (cond ((not (equal result ""))
  1444.                     nil)
  1445.                    ((not require-match)
  1446.                     (setq result default)
  1447.                     nil)
  1448.                    ((not default)
  1449.                     t)
  1450.                    ((not (get-buffer default))
  1451.                     t)
  1452.                    (t
  1453.                     (setq result default)
  1454.                     nil))))
  1455.     (if (bufferp result)
  1456.         (buffer-name result)
  1457.       result)))
  1458.  
  1459. (defun read-number (prompt &optional integers-only)
  1460.   "Reads a number from the minibuffer."
  1461.   (let ((pred (if integers-only 'integerp 'numberp))
  1462.     num)
  1463.     (while (not (funcall pred num))
  1464.       (setq num (condition-case ()
  1465.             (let ((minibuffer-completion-table nil))
  1466.               (read-from-minibuffer
  1467.                prompt (if num (prin1-to-string num)) nil t
  1468.                t)) ;no history
  1469.           (invalid-read-syntax nil)
  1470.           (end-of-file nil)))
  1471.       (or (funcall pred num) (beep)))
  1472.     num))
  1473.  
  1474. (defun read-shell-command (prompt &optional initial-input history)
  1475.   "Just like read-string, but uses read-shell-command-map:
  1476. \\{read-shell-command-map}"
  1477.   (let ((minibuffer-completion-table nil))
  1478.     (read-from-minibuffer prompt initial-input read-shell-command-map
  1479.               nil (or history 'shell-command-history))))
  1480.  
  1481.  
  1482. ;;; This read-file-name stuff probably belongs in files.el
  1483.  
  1484. ;; Quote "$" as "$$" to get it past substitute-in-file-name
  1485. (defun un-substitute-in-file-name (string)
  1486.   (let ((regexp "\\$")
  1487.         (olen (length string))
  1488.         new
  1489.         n o ch)
  1490.     (cond ((eq system-type 'vax-vms)
  1491.            string)
  1492.           ((not (string-match regexp string))
  1493.            string)
  1494.           (t
  1495.            (setq n 1)
  1496.            (while (string-match regexp string (match-end 0))
  1497.              (setq n (1+ n)))
  1498.            (setq new (make-string (+ olen n) ?$))
  1499.            (setq n 0 o 0)
  1500.            (while (< o olen)
  1501.              (setq ch (aref string o))
  1502.              (aset new n ch)
  1503.              (setq o (1+ o) n (1+ n))
  1504.              (if (eq ch ?$)
  1505.                  ;; already aset by make-string initial-value
  1506.                  (setq n (1+ n))))
  1507.            new))))
  1508.   
  1509. (defun read-file-name-2 (history prompt dir default 
  1510.                  must-match initial-contents
  1511.                  completer)
  1512.   (if (not dir)
  1513.       (setq dir default-directory))
  1514.   (setq dir (abbreviate-file-name dir t))
  1515.   (let* ((insert (cond ((and (not insert-default-directory)
  1516.                  (not initial-contents))
  1517.                         "")
  1518.                        (initial-contents
  1519.                         (cons (un-substitute-in-file-name
  1520.                    (concat dir initial-contents))
  1521.                               (length dir)))
  1522.                        (t
  1523.                         (un-substitute-in-file-name dir))))
  1524.          (val (let ((completion-ignore-case (or completion-ignore-case
  1525.                         (eq system-type 'vax-vms))))
  1526.                 ;;  Hateful, broken, case-sensitive un*x
  1527. ;;;                 (completing-read prompt
  1528. ;;;                                  completer
  1529. ;;;                                  dir
  1530. ;;;                                  must-match
  1531. ;;;                                  insert
  1532. ;;;                                  history)
  1533.         ;; #### - this is essentially the guts of completing read.
  1534.         ;; There should be an elegant way to pass a pair of keymaps to
  1535.         ;; completing read, but this will do for now.  All sins are
  1536.         ;; relative.  --Stig
  1537.         (let ((minibuffer-completion-table completer)
  1538.               (minibuffer-completion-predicate dir)
  1539.               (minibuffer-completion-confirm (if (eq must-match 't)
  1540.                              nil t))
  1541.               (last-exact-completion nil))
  1542.           (read-from-minibuffer prompt
  1543.                     insert
  1544.                     (if (not must-match)
  1545.                         read-file-name-map
  1546.                       read-file-name-must-match-map)
  1547.                     nil
  1548.                     history)))
  1549.           ))
  1550. ;;;     ;; Kludge!  Put "/foo/bar" on history rather than "/default//foo/bar"
  1551. ;;;     (let ((hist (cond ((not history) 'minibuffer-history)
  1552. ;;;                       ((consp history) (car history))
  1553. ;;;                       (t history))))
  1554. ;;;       (if (and val
  1555. ;;;                hist
  1556. ;;;                (not (eq hist 't))
  1557. ;;;                (boundp hist)
  1558. ;;;                (equal (car-safe (symbol-value hist)) val))
  1559. ;;;           (let ((e (condition-case nil
  1560. ;;;                        (expand-file-name val)
  1561. ;;;                      (error nil))))
  1562. ;;;             (if (and e (not (equal e val)))
  1563. ;;;                 (set hist (cons e (cdr (symbol-value hist))))))))
  1564.  
  1565.     (cond ((not val)
  1566.            (error "No file name specified"))
  1567.           ((and default
  1568.                 (equal val (if (consp insert) (car insert) insert)))
  1569.            default)
  1570.           (t
  1571.            (substitute-in-file-name val)))))
  1572.  
  1573. ;; #### this function should use minibuffer-completion-table
  1574. ;; or something.  But that is sloooooow.
  1575. ;; #### all this shit needs better documentation!!!!!!!!
  1576. (defun read-file-name-activate-callback (event extent dir-p)
  1577.   ;; used as the activate-callback of the filename list items
  1578.   ;; in the completion buffer, in place of default-choose-completion.
  1579.   ;; if a regular file was selected, we call default-choose-completion
  1580.   ;; (which just inserts the string in the minibuffer and calls
  1581.   ;; exit-minibuffer).  If a directory was selected, we display
  1582.   ;; the contents of the directory.
  1583.   (let* ((file (extent-string extent))
  1584.      (completion-buf (extent-object extent))
  1585.      (minibuf (symbol-value-in-buffer 'completion-reference-buffer
  1586.                       completion-buf))
  1587.      (in-dir (file-name-directory (buffer-substring nil nil minibuf)))
  1588.      (full (expand-file-name file in-dir)))
  1589.     (if (not (file-directory-p full))
  1590.     (default-choose-completion event extent minibuf)
  1591.       (erase-buffer minibuf)
  1592.       (insert-string (file-name-as-directory
  1593.               (abbreviate-file-name full t)) minibuf)
  1594.       (reset-buffer completion-buf)
  1595.       (let ((standard-output completion-buf))
  1596.     (display-completion-list
  1597.      (delete "." (directory-files full nil nil nil (if dir-p 'directory)))
  1598.      :user-data dir-p
  1599.      :reference-buffer minibuf
  1600.      :activate-callback 'read-file-name-activate-callback)
  1601.     (goto-char (point-min) completion-buf)))))
  1602.  
  1603. (defun read-file-name-1 (history prompt dir default 
  1604.                  must-match initial-contents
  1605.                  completer)
  1606.   (if (should-use-dialog-box-p)
  1607.       ;; this calls read-file-name-2
  1608.       (mouse-read-file-name-1 history prompt dir default must-match
  1609.                   initial-contents completer)
  1610.     (let ((rfhookfun
  1611.        (lambda ()
  1612.          (set
  1613.           (make-local-variable
  1614.            'completion-display-completion-list-function)
  1615.           #'(lambda (completions)
  1616.           (display-completion-list
  1617.            completions
  1618.            :user-data (not (eq completer 'read-file-name-internal))
  1619.            :activate-callback
  1620.            'read-file-name-activate-callback)))
  1621.          ;; kludge!
  1622.          (remove-hook 'minibuffer-setup-hook rfhookfun)
  1623.          )))
  1624.       (unwind-protect
  1625.       (progn
  1626.         (add-hook 'minibuffer-setup-hook rfhookfun)
  1627.         (read-file-name-2 history prompt dir default must-match
  1628.                   initial-contents completer))
  1629.     (remove-hook 'minibuffer-setup-hook rfhookfun)))))
  1630.  
  1631. (defun read-file-name (prompt
  1632.                        &optional dir default must-match initial-contents
  1633.                history)
  1634.   "Read file name, prompting with PROMPT and completing in directory DIR.
  1635. This will prompt with a dialog box if appropriate, according to
  1636.  `should-use-dialog-box-p'.
  1637. Value is not expanded---you must call `expand-file-name' yourself.
  1638. Value is subject to interpreted by substitute-in-file-name however.
  1639. Default name to DEFAULT if user enters a null string.
  1640.  (If DEFAULT is omitted, the visited file name is used,
  1641.   except that if INITIAL-CONTENTS is specified, that combined with DIR is
  1642.   used.)
  1643. Fourth arg MUST-MATCH non-nil means require existing file's name.
  1644.  Non-nil and non-t means also require confirmation after completion.
  1645. Fifth arg INITIAL-CONTENTS specifies text to start with.
  1646. Sixth arg HISTORY specifies the history list to use.  Default is
  1647.  `file-name-history'.
  1648. DIR defaults to current buffer's directory default."
  1649.   (read-file-name-1
  1650.    (or history 'file-name-history)
  1651.    prompt dir (or default
  1652.           (if initial-contents (expand-file-name initial-contents dir)
  1653.             buffer-file-name))
  1654.    must-match initial-contents
  1655.    ;; A separate function (not an anonymous lambda-expression)
  1656.    ;; and passed as a symbol because of disgusting kludges in various
  1657.    ;; places which do stuff like (let ((filename-kludge-p (eq minibuffer-completion-table 'read-file-name-internal))) ...)
  1658.    'read-file-name-internal))
  1659.  
  1660. (defun read-directory-name (prompt
  1661.                             &optional dir default must-match initial-contents
  1662.                 history)
  1663.   "Read directory name, prompting with PROMPT and completing in directory DIR.
  1664. This will prompt with a dialog box if appropriate, according to
  1665.  `should-use-dialog-box-p'.
  1666. Value is not expanded---you must call `expand-file-name' yourself.
  1667. Value is subject to interpreted by substitute-in-file-name however.
  1668. Default name to DEFAULT if user enters a null string.
  1669.  (If DEFAULT is omitted, the current buffer's default directory is used.)
  1670. Fourth arg MUST-MATCH non-nil means require existing directory's name.
  1671.  Non-nil and non-t means also require confirmation after completion.
  1672. Fifth arg INITIAL-CONTENTS specifies text to start with.
  1673. Sixth arg HISTORY specifies the history list to use.  Default is
  1674.  `file-name-history'.
  1675. DIR defaults to current buffer's directory default."
  1676.   (read-file-name-1 
  1677.     (or history 'file-name-history)
  1678.     prompt dir (or default default-directory) must-match initial-contents
  1679.     'read-directory-name-internal))
  1680.  
  1681.  
  1682. ;; Environment-variable completion hack
  1683. (defun read-file-name-internal-1 (string dir action completer)
  1684.   (if (not (string-match
  1685.         "\\([^$]\\|\\`\\)\\(\\$\\$\\)*\\$\\([A-Za-z0-9_]*\\|{[^}]*\\)\\'"
  1686.         string))
  1687.       ;; Not doing environment-variable completion hack
  1688.       (let* ((orig (if (equal string "") nil string))
  1689.              (sstring (if orig (substitute-in-file-name string) string))
  1690.              (specdir (if orig (file-name-directory sstring) nil)))
  1691.         (funcall completer 
  1692.                  action 
  1693.                  orig 
  1694.                  sstring 
  1695.                  specdir
  1696.                  (if specdir (expand-file-name specdir dir) dir)
  1697.                  (if orig (file-name-nondirectory sstring) string)))
  1698.       ;; An odd number of trailing $'s
  1699.       (let* ((start (match-beginning 3))
  1700.              (env (substring string 
  1701.                              (cond ((= start (length string))
  1702.                                     ;; "...$"
  1703.                                     start)
  1704.                                    ((= (aref string start) ?{)
  1705.                                     ;; "...${..."
  1706.                                     (1+ start))
  1707.                                    (t
  1708.                                     start))))
  1709.              (head (substring string 0 (1- start)))
  1710.              (alist #'(lambda ()
  1711.                         (mapcar #'(lambda (x)
  1712.                                     (cons (substring x 0 (string-match "=" x))
  1713.                                           'nil))
  1714.                                 process-environment))))
  1715.         
  1716.     (cond ((eq action 'lambda)
  1717.                nil)
  1718.               ((eq action 't)
  1719.                ;; all completions
  1720.                (mapcar #'(lambda (p)
  1721.                (if (and (> (length p) 0)
  1722.                     ;;#### Unix-specific
  1723.                     ;;####  -- need absolute-pathname-p
  1724.                     (/= (aref p 0) ?/))
  1725.                    (concat "$" p)
  1726.                              (concat head "$" p)))
  1727.                        (all-completions env (funcall alist))))
  1728.               (t ;; 'nil
  1729.                ;; complete
  1730.                (let* ((e (funcall alist))
  1731.                       (val (try-completion env e)))
  1732.                  (cond ((stringp val)
  1733.                         (if (string-match "[^A-Za-z0-9_]" val)
  1734.                             (concat head
  1735.                                     "${" val
  1736.                                     ;; completed uniquely?
  1737.                                     (if (eq (try-completion val e) 't)
  1738.                                         "}" ""))
  1739.                             (concat head "$" val)))
  1740.                        ((eql val 't)
  1741.                         (concat head
  1742.                                 (un-substitute-in-file-name (getenv env))))
  1743.                        (t nil))))))))
  1744.  
  1745.  
  1746. (defun read-file-name-internal (string dir action)
  1747.   (read-file-name-internal-1 
  1748.    string dir action
  1749.    #'(lambda (action orig string specdir dir name)
  1750.       (cond ((eq action 'lambda)
  1751.              (if (not orig)
  1752.                  nil
  1753.                (let ((sstring (condition-case nil 
  1754.                                   (expand-file-name string)
  1755.                                 (error nil))))
  1756.                  (if (not sstring)
  1757.                      ;; Some pathname syntax error in string
  1758.                      nil
  1759.                      (file-exists-p sstring)))))
  1760.             ((eq action 't)
  1761.              ;; all completions
  1762.              (mapcar #'un-substitute-in-file-name
  1763.                      (file-name-all-completions name dir)))
  1764.             (t;; 'nil
  1765.              ;; complete
  1766.              (let* ((d (or dir default-directory))
  1767.             (val (file-name-completion name d)))
  1768.                (if (and (eq val 't)
  1769.                         (not (null completion-ignored-extensions)))
  1770.                    ;;#### (file-name-completion "foo") returns 't
  1771.                    ;;   when both "foo" and "foo~" exist and the latter
  1772.                    ;;   is "pruned" by completion-ignored-extensions.
  1773.                    ;; I think this is a bug in file-name-completion.
  1774.                    (setq val (let ((completion-ignored-extensions '()))
  1775.                                (file-name-completion name d))))
  1776.                (if (stringp val)
  1777.                    (un-substitute-in-file-name (if specdir
  1778.                                                    (concat specdir val)
  1779.                                                    val))
  1780.                    (let ((tem (un-substitute-in-file-name string)))
  1781.                      (if (not (equal tem orig))
  1782.                          ;; substitute-in-file-name did something
  1783.                          tem
  1784.                          val)))))))))
  1785.  
  1786. (defun read-directory-name-internal (string dir action)
  1787.   (read-file-name-internal-1 
  1788.    string dir action
  1789.    #'(lambda (action orig string specdir dir name)
  1790.       (let* ((dirs #'(lambda (fn)
  1791.                (let ((l (if (equal name "")
  1792.                     (directory-files
  1793.                      dir
  1794.                      nil
  1795.                      ""
  1796.                      nil
  1797.                      'directories)
  1798.                   (directory-files
  1799.                    dir
  1800.                    nil 
  1801.                    (concat "\\`" (regexp-quote name))
  1802.                    nil
  1803.                    'directories))))
  1804.              (mapcar fn
  1805.                  (cond ((eq system-type 'vax-vms)
  1806.                     l)
  1807.                        (t
  1808.                     ;; Wretched unix
  1809.                     (delete "." l))))))))
  1810.         (cond ((eq action 'lambda)
  1811.                ;; complete?
  1812.                (if (not orig)
  1813.                    nil
  1814.          (file-directory-p string)))
  1815.               ((eq action 't)
  1816.                ;; all completions
  1817.                (funcall dirs #'(lambda (n)
  1818.                  (un-substitute-in-file-name 
  1819.                   (file-name-as-directory n)))))
  1820.               (t
  1821.                ;; complete
  1822.                (let ((val (try-completion
  1823.                            name
  1824.                            (funcall dirs
  1825.                                     #'(lambda (n)
  1826.                     (list (file-name-as-directory
  1827.                            n)))))))
  1828.                  (if (stringp val)
  1829.                      (un-substitute-in-file-name (if specdir
  1830.                                                      (concat specdir val)
  1831.                            val))
  1832.            (let ((tem (un-substitute-in-file-name string)))
  1833.              (if (not (equal tem orig))
  1834.              ;; substitute-in-file-name did something
  1835.              tem
  1836.                val))))))))))
  1837.  
  1838. (defun append-expand-filename (file-string string)
  1839.   "Append STRING to FILE-STRING differently depending on whether STRING
  1840. is a username (~string), an environment variable ($string), 
  1841. or a filename (/string).  The resultant string is returned with the 
  1842. environment variable or username expanded and resolved to indicate 
  1843. whether it is a file(/result) or a directory (/result/)."
  1844.   (let ((file 
  1845.      (cond ((string-match "\\([~$]\\)\\([^~$/]*\\)$" file-string)
  1846.         (cond ((string= (substring file-string 
  1847.                        (match-beginning 1)
  1848.                        (match-end 1)) "~")
  1849.                (concat (substring file-string 0 (match-end 1))
  1850.                    string))
  1851.               (t (substitute-in-file-name
  1852.               (concat (substring file-string 0 (match-end 1))
  1853.                   string)))))
  1854.            (t (concat (file-name-directory 
  1855.                (substitute-in-file-name file-string)) string))))
  1856.     result)
  1857.     
  1858.     (cond ((stringp (setq result (and (file-exists-p (expand-file-name file))
  1859.                       (read-file-name-internal 
  1860.                        (condition-case nil
  1861.                        (expand-file-name file)
  1862.                      (error file))
  1863.                        "" nil))))
  1864.        result)
  1865.       (t file))))
  1866.  
  1867. (defun mouse-file-display-completion-list (window dir minibuf user-data)
  1868.   (let ((standard-output (window-buffer window)))
  1869.     (condition-case nil
  1870.     (display-completion-list 
  1871.      (directory-files dir nil nil nil t)
  1872.      :window-width (* 2 (window-width window))
  1873.      :activate-callback
  1874.      'mouse-read-file-name-activate-callback
  1875.      :user-data user-data
  1876.      :reference-buffer minibuf
  1877.      :help-string "")
  1878.       (t nil))))
  1879.  
  1880. (defun mouse-directory-display-completion-list (window dir minibuf user-data)
  1881.   (let ((standard-output (window-buffer window)))
  1882.     (condition-case nil
  1883.     (display-completion-list
  1884.      (delete "." (directory-files dir nil nil nil 1))
  1885.      :window-width (window-width window)
  1886.      :activate-callback
  1887.      'mouse-read-file-name-activate-callback
  1888.      :user-data user-data
  1889.      :reference-buffer minibuf
  1890.      :help-string "")
  1891.       (t nil))))
  1892.  
  1893. (defun mouse-read-file-name-activate-callback (event extent user-data)
  1894.   (let* ((file (extent-string extent))
  1895.      (minibuf (symbol-value-in-buffer 'completion-reference-buffer
  1896.                       (extent-object extent)))
  1897.      (in-dir (buffer-substring nil nil minibuf))
  1898.      (full (expand-file-name file in-dir))
  1899.      (filebuf (nth 0 user-data))
  1900.      (dirbuff (nth 1 user-data))
  1901.      (filewin (nth 2 user-data))
  1902.      (dirwin (nth 3 user-data)))
  1903.     (if (file-regular-p full)
  1904.     (default-choose-completion event extent minibuf)
  1905.       (erase-buffer minibuf)
  1906.       (insert-string (file-name-as-directory
  1907.               (abbreviate-file-name full t)) minibuf)
  1908.       (reset-buffer filebuf)
  1909.       (if (not dirbuff)
  1910.       (mouse-directory-display-completion-list filewin full minibuf
  1911.                            user-data)
  1912.     (mouse-file-display-completion-list filewin full minibuf user-data)
  1913.     (reset-buffer dirbuff)
  1914.     (mouse-directory-display-completion-list dirwin full minibuf
  1915.                          user-data)))))
  1916.  
  1917. ;; this is rather cheesified but gets the job done.
  1918. (defun mouse-read-file-name-1 (history prompt dir default 
  1919.                  must-match initial-contents
  1920.                  completer)
  1921.   (let* ((file-p (eq 'read-file-name-internal completer))
  1922.      (filebuf (get-buffer-create "*Completions*"))
  1923.      (dirbuff (and file-p (generate-new-buffer " *mouse-read-file*")))
  1924.      (butbuff (generate-new-buffer " *mouse-read-file*"))
  1925.      (frame (make-dialog-frame))
  1926.      filewin dirwin
  1927.      user-data)
  1928.     (unwind-protect
  1929.     (progn
  1930.       (reset-buffer filebuf)
  1931.       (select-frame frame)
  1932.       (let ((window-min-height 1))
  1933.         ;; #### should be 2 not 3, but that causes
  1934.         ;; "window too small to split" errors for some
  1935.         ;; people (but not for me ...) There's a more
  1936.         ;; fundamental bug somewhere.
  1937.         (split-window nil (- (frame-height frame) 3)))
  1938.       (if file-p
  1939.           (progn
  1940.         (split-window-horizontally 16)
  1941.         (setq filewin (frame-rightmost-window frame)
  1942.               dirwin (frame-leftmost-window frame))
  1943.         (set-window-buffer filewin filebuf)
  1944.         (set-window-buffer dirwin dirbuff))
  1945.         (setq filewin (frame-highest-window frame))
  1946.         (set-window-buffer filewin filebuf))
  1947.       (setq user-data (list filebuf dirbuff filewin dirwin))
  1948.       (set-window-buffer (frame-lowest-window frame) butbuff)
  1949.       (set-buffer butbuff)
  1950.       (when (featurep 'scrollbar)
  1951.         (set-specifier scrollbar-width 0 butbuff))
  1952.       (insert "                 ")
  1953.       (insert-gui-button (make-gui-button "OK" 
  1954.                           (lambda (foo)
  1955.                         (exit-minibuffer))))
  1956.       (insert "                 ")
  1957.       (insert-gui-button (make-gui-button "Cancel"
  1958.                           (lambda (foo)
  1959.                         (abort-recursive-edit))))
  1960.       (let ((rfhookfun
  1961.          (lambda ()
  1962.            (if (not file-p)
  1963.                (mouse-directory-display-completion-list
  1964.             filewin dir (current-buffer) user-data)
  1965.              (mouse-file-display-completion-list filewin dir
  1966.                              (current-buffer)
  1967.                              user-data)
  1968.              (mouse-directory-display-completion-list dirwin dir
  1969.                                   (current-buffer)
  1970.                                   user-data))
  1971.            (set
  1972.             (make-local-variable
  1973.              'completion-display-completion-list-function)
  1974.             #'(lambda (completions)
  1975.             (display-completion-list
  1976.              completions
  1977.              :help-string ""
  1978.              :activate-callback
  1979.              'mouse-read-file-name-activate-callback
  1980.              :user-data user-data)))
  1981.            ;; kludge!
  1982.            (remove-hook 'minibuffer-setup-hook rfhookfun)
  1983.            ))
  1984.         (rfcshookfun
  1985.          ;; kludge!
  1986.          ;; #### I really need to flesh out the object
  1987.          ;; hierarchy better to avoid these kludges.
  1988.          (lambda ()
  1989.            (save-excursion
  1990.              (set-buffer standard-output)
  1991.              (setq truncate-lines t)))))
  1992.         (unwind-protect
  1993.         (progn
  1994.           (add-hook 'minibuffer-setup-hook rfhookfun)
  1995.           (add-hook 'completion-setup-hook rfcshookfun)
  1996.           (read-file-name-2 history prompt dir default 
  1997.                     must-match initial-contents
  1998.                     completer))
  1999.           (remove-hook 'minibuffer-setup-hook rfhookfun)
  2000.           (remove-hook 'completion-setup-hook rfcshookfun))))
  2001.       (delete-frame frame)
  2002.       (kill-buffer filebuf)
  2003.       (kill-buffer butbuff)
  2004.       (and dirbuff (kill-buffer dirbuff)))))
  2005.  
  2006. (defun read-face (prompt &optional must-match)
  2007.   "Read the name of a face from the minibuffer and return it as a symbol."
  2008.   (intern (completing-read prompt obarray 'find-face must-match)))
  2009.  
  2010. ;; #### - wrong place for this variable?  Exactly.  We probably want
  2011. ;; `color-list' to be a console method, so `tty-color-list' becomes
  2012. ;; obsolete, and `read-color-completion-table' conses (mapcar #'list
  2013. ;; (color-list)), optionally caching the results.
  2014.  
  2015. ;; Ben wanted all of the possibilities from the `configure' script used
  2016. ;; here, but I think this is way too many.  I already trimmed the R4 variants
  2017. ;; and a few obvious losers from the list.  --Stig  
  2018. (defvar x-library-search-path '("/usr/X11R6/lib/X11/"
  2019.                 "/usr/X11R5/lib/X11/"
  2020.                 "/usr/lib/X11R6/X11/"
  2021.                 "/usr/lib/X11R5/X11/"
  2022.                 "/usr/local/X11R6/lib/X11/"
  2023.                 "/usr/local/X11R5/lib/X11/"
  2024.                 "/usr/local/lib/X11R6/X11/"
  2025.                 "/usr/local/lib/X11R5/X11/"
  2026.                 "/usr/X11/lib/X11/"
  2027.                 "/usr/lib/X11/"
  2028.                 "/usr/local/lib/X11/"
  2029.                 "/usr/X386/lib/X11/"
  2030.                 "/usr/x386/lib/X11/"
  2031.                 "/usr/XFree86/lib/X11/"
  2032.                 "/usr/unsupported/lib/X11/"
  2033.                 "/usr/athena/lib/X11/"
  2034.                 "/usr/local/x11r5/lib/X11/"
  2035.                 "/usr/lpp/Xamples/lib/X11/"
  2036.                 "/usr/openwin/lib/X11/"
  2037.                 "/usr/openwin/share/lib/X11/")
  2038.   "Search path used by `read-color' to find rgb.txt.") 
  2039.  
  2040. (defvar read-color-completion-table)
  2041.  
  2042. (defun read-color-completion-table ()
  2043.   (if (boundp 'read-color-completion-table)
  2044.       read-color-completion-table
  2045.       (let ((rgb-file (locate-file "rgb.txt" x-library-search-path))
  2046.         clist color p)
  2047.     (if (not rgb-file)
  2048.         ;; prevents multiple searches for rgb.txt if we can't find it
  2049.         (setq read-color-completion-table nil)
  2050.       (save-excursion
  2051.         (set-buffer (get-buffer-create " *colors*"))
  2052.         (reset-buffer (current-buffer))
  2053.         (insert-file-contents rgb-file)
  2054.         (while (not (eobp))
  2055.           ;; skip over comments
  2056.           (while (looking-at "^!")
  2057.         (end-of-line)
  2058.         (forward-char 1))
  2059.           (skip-chars-forward "0-9 \t")
  2060.           (setq p (point))
  2061.           (end-of-line)
  2062.           (setq color (buffer-substring p (point))
  2063.             clist (cons (list color) clist))
  2064.           ;; Ugh.  If we want to be able to complete the lowercase form
  2065.           ;; of the color name, we need to add it twice!  Yuck.
  2066.               (let ((dcase (downcase color)))
  2067.                 (or (string= dcase color)
  2068.                     (setq clist (cons (list dcase) clist))))
  2069.           (forward-char 1))
  2070.         (kill-buffer (current-buffer))))
  2071.     (setq read-color-completion-table clist)
  2072.     read-color-completion-table)))
  2073.  
  2074. (defun read-color (prompt &optional must-match initial-contents)
  2075.   "Read the name of a color from the minibuffer.
  2076. Uses `x-library-search-path' to find rgb.txt in order to build a completion
  2077. table."
  2078.   (completing-read prompt (read-color-completion-table) nil
  2079.            (and (read-color-completion-table) must-match)
  2080.            initial-contents))
  2081.  
  2082.  
  2083. ;; #### The doc string for read-non-nil-coding system gets lost if we
  2084. ;; only include these if the mule feature is present.  Strangely,
  2085. ;; read-coding-system doesn't.
  2086.  
  2087. ;;(if (featurep 'mule)
  2088.  
  2089. (defun read-coding-system (prompt)
  2090.   "Read a coding-system (or nil) from the minibuffer.
  2091. Prompting with string PROMPT."
  2092.   (intern (completing-read prompt obarray 'find-coding-system t)))
  2093.  
  2094. (defun read-non-nil-coding-system (prompt)
  2095.   "Read a non-nil coding-system from the minibuffer.
  2096. Prompt with string PROMPT."
  2097.   (let ((retval (intern "")))
  2098.     (while (= 0 (length (symbol-name retval)))
  2099.       (setq retval (intern (completing-read prompt obarray
  2100.                         'find-coding-system
  2101.                         t))))
  2102.     retval))
  2103.  
  2104. ;;) ;; end of (featurep 'mule)
  2105.  
  2106.  
  2107.  
  2108. (defcustom force-dialog-box-use nil
  2109.   "*If non-nil, always use a dialog box for asking questions, if possible.
  2110. You should *bind* this, not set it.  This is useful if you're doing
  2111. something mousy but which wasn't actually invoked using the mouse."
  2112.   :type 'boolean
  2113.   :group 'minibuffer)
  2114.  
  2115. ;; We include this here rather than dialog.el so it is defined
  2116. ;; even when dialog boxes are not present.
  2117. (defun should-use-dialog-box-p ()
  2118.   "If non-nil, questions should be asked with a dialog box instead of the
  2119. minibuffer.  This looks at `last-command-event' to see if it was a mouse
  2120. event, and checks whether dialog-support exists and the current device
  2121. supports dialog boxes.
  2122.  
  2123. The dialog box is totally disabled if the variable `use-dialog-box'
  2124. is set to nil."
  2125.   (and (featurep 'dialog)
  2126.        (device-on-window-system-p)
  2127.        use-dialog-box
  2128.        (or force-dialog-box-use
  2129.        (button-press-event-p last-command-event)
  2130.        (button-release-event-p last-command-event)
  2131.        (misc-user-event-p last-command-event))))
  2132.  
  2133. ;;; minibuf.el ends here
  2134.